﻿@{
    ViewBag.title = "班级编辑";
}
@section headerSection{
    <style>
        .form-group {
            margin-bottom: 8px;
        }
    </style>
}

<div class="panel-body autoHeight" style="width:700px;">
    <form id="editForm" class="form-horizontal m-t">
        <div class="form-group">
            <input type="hidden" id="id" name="id" value="@ViewBag.id" />
            <label class="col-sm-4 control-label">所属学校<span class="glyphicon glyphicon-star-empty" style="color:red;" aria-hidden="true"></span></label>
            <div class="col-sm-8">
                <input type="hidden" name="schoolCode" id="schoolCode" />
                <select id="schoolName" name="schoolName" required aria-required="true" title="所属学校" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                    <option></option>
                   @for (var i = 0; i < ViewBag.units.Count; i++) 
                   {
                    <option value="@ViewBag.units[i].text" code="@ViewBag.units[i].value">@ViewBag.units[i].text</option>
                   }
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">班级名称<span class="glyphicon glyphicon-star-empty" style="color:red;" aria-hidden="true"></span></label>
            <div class="col-sm-8">
                <input type="text" name="className" class="form-control input-sm" autocomplete="off"
                       placeholder="请输入班级名称" title="请输入班级名称" required="" aria-required="true" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">入学年份</label>
            <div class="col-sm-8">
                <input type="text" id="entranceYear" name="entranceYear" class="form-control input-sm"
                       autocomplete="off"
                       onkeyup="this.value=this.value.replace(/\D/g,'')"
                       onafterpaste="this.value=this.value.replace(/\D/g,'')"
                       placeholder="请输入入学年份" title="请输入入学年份" />
            </div>
        </div>
        
        <div class="form-group">
            <label class="col-sm-4 control-label">班主任</label>
            <div class="col-sm-8">
                <input type="hidden" id="teacherUserId" name="teacherUserId" />
                <select id="teacherName" name="teacherName" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;"></select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">人数</label>
            <div class="col-sm-8">
                <input type="text" id="classStudentNum" name="classStudentNum" class="form-control input-sm" autocomplete="off"
                       onkeyup="this.value=this.value.replace(/\D/g,'')"
                       onafterpaste="this.value=this.value.replace(/\D/g,'')"
                       placeholder="请输入人数" title="请输入人数" />
            </div>
        </div>
       
        <div class="form-group">
            <label class="col-sm-4 control-label">备注</label>
            <div class="col-sm-8">
                <textarea id="remark" autocomplete="off" name="remark" placeholder="请输入备注" title="请输入备注" rows="3" class="form-control"></textarea>
            </div>
        </div>
        <div id="saveContent" class="form-group">
            <div class="col-sm-4 col-sm-offset-4">
                <button id="save" type="button" class="btn btn-w-m btn-primary">
                    保存
                </button>
            </div>
        </div>
    </form>
</div>

@section footSection{
    <script src="~/Resources/tools/jqueryExtend.js"></script>
    <script>
        var urlRoot = ctx + '/SchoolClass/';
        function loadTeacher(unit) {
            $.post(urlRoot + 'getTeacher', { unit: unit }, function (server) {
                if (server.Statu == 0) {
                    for (var i in server.Data) {
                        $('#teacherName').append($('<option value="' + server.Data[i].userName + '" userId="'+server.Data[i].userId+'" >' + server.Data[i].userName + '(登录名：' + server.Data[i].loginName + ')' + '</option>'));
                    }
                } else {
                    layer.alert('加载班主任列表失败', { icon: 5 });
                }
            });
        }

        function find() {
            var _id = $('#id').val();
            if (_id != "") {
                $.post(urlRoot + 'find', { id: _id }, function (server) {
                    if (server.Statu == 0) {
                        //编辑时，加载班级所属学校备选班主任列表
                        var teachers = server.Data.teacherSelectList;
                        for (var i in teachers) {
                            $('#teacherName').append($('<option value="' + teachers[i].userName + '" userId="' + teachers[i].userId + '" >' + teachers[i].userName + '(登录名：' + teachers[i].loginName + ')' + '</option>'));
                        }
                        $('#editForm').LoadForm(server.Data);
                    } else {
                        layer.alert('加载数据失败', { icon: 5 });
                    }
                });
            }
        }

        function save() {
            if ($('#editForm').valid()) {
                var PIndex;

                var code = $('#schoolName option:selected').attr('code');
                $('#schoolCode').val(code);

                var userId = $('#teacherName option:selected').attr('userId');
                $('#teacherUserId').val(userId);

                $('#editForm').ajaxSubmit({
                    url: urlRoot + 'save',
                    type: "post",
                    dataType: "json",
                    beforeSubmit: function (arr, $form, options) {
                        PIndex = layer.msg('正在提交数据', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
                    },
                    success: function (result, status, xhr, $form) {
                        layer.close(PIndex);
                        if (result.Statu === 0) {
                            $('#id').val(result.Data);
                            layer.msg('保存成功', { icon: 1 });
                        } else if (result.Statu == 2) {
                            layer.alert(result.Msg, { icon: 7 });
                        }
                        else
                            layer.alert('保存失败', { icon: 5 });
                    },
                    error: function (xhr, status, error, $form) {
                        layer.close(PIndex);
                        layer.alert("保存失败", { icon: 5 });
                    }
                });
            }
        }

        $(function () {
            $('#schoolName').change(function () {
                loadTeacher($(this).val());
            });

            $('#save').click(function () {
                save();
            });

            find();
        });
    </script>
}
